#include "../daemon/Daemon.hpp"
#include "TcpServer.hpp"
#include "Usage.hpp"
//#include "Protocol.hpp"
#include "string"
#include "Protocol.hpp"



std::string HandlerFunction(Socket *socket)
{
    std::unique_ptr<Factory> factory = std::make_unique<Factory>();
    // std::shared_ptr<Protocol> req = factory->BuildRequest();
    Protocol req;
    recv(socket->GetSockfd(), &(req), sizeof(req), 0);
    int result = req.Result();
    std::shared_ptr<Result> res = factory->BuildResult(result, 0);
    send(socket->GetSockfd(), &(*res), sizeof(*res), 0);
    return "success";
}

// ./tcpserver serverport
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(-1);
    }
    //Daemon daemon{};
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>(HandlerFunction);
    svr->Init(port);
    svr->Start();
    return 0;
}